﻿#region Includes
using System;
using System.Drawing;
using System.Collections.Generic;
#endregion

namespace GingerGL.Graphics
{
    //------------------------------------------------------------//
    /// <summary>
    /// Material Manager
    /// </summary>
    public class MaterialManager
    {
        //--------------------------------------------------------//
        #region Variables
        private List<Material> materials = new List<Material>();
        #endregion
        //--------------------------------------------------------//
        #region Methods
        /// <summary>
        /// Gets/Adds Material from Manager
        /// </summary>
        /// <param name="mat">to compare</param>
        public Material GetMaterial(Material mat)
        {
            foreach (Material m in materials)                   //cycle thur it all
                if (SameMat(m, mat))                            //compare
                    return m;                                   //found!
            materials.Add(mat);                                 //no luck, add
            return mat;                                         //return material
        }
        /// <summary>
        /// Gets matertial by name
        /// </summary>
        public Material GetMaterial(string name)
        {
            foreach (Material m in materials)                   //cycle thru list
                if (m.Name == name)                             //and compare name
                    return m;                                   //return it found
            return null;                                        //return null if not
        }
        /// <summary>
        /// Creates Material
        /// </summary>
        public Material CreateNewMaterial()
        {
            Material mat = new Material();                          //make it
            materials.Add(mat);                                     //add it
            return mat;                                             //return it
        }
        /// <summary>
        /// Creates Material from image
        /// </summary>
        public Material FromImage(Image image)
        {
            Material mat = new Material(image);                     //make it
            materials.Add(mat);                                     //add it
            return mat;                                             //return it
        }
        /// <summary>
        /// Creates Material from file
        /// </summary>
        public Material FromTextureFile(string filename, bool clamp)
        {
            Material mat = new Material();                          //make it
            mat.Name = filename;                                    //set it
            mat.Texture = Texture.Instance.GetTexture(filename, clamp);//load it
            materials.Add(mat);                                     //add it
            return mat;                                             //return it
        }
        /// <summary>
        /// Invlaidate Everything
        /// </summary>
        public void Invalidate()
        {
            foreach (Material m in materials)                       //cycle thru the list
                m.Invalidate();                                     //and invalidate everything
        }
        /// <summary>
        /// Clear everything
        /// </summary>
        public void Flush()
        {
            Invalidate();                                           //Invalidate it
            materials.Clear();                                      //then clear the list
        }
        /// <summary>
        /// Compare 2 Mats
        /// </summary>
        private bool SameMat(Material m1, Material m2)
        {
            if (m1.Name != m2.Name) return false;                   //yep, compare it all
            if (m1.Texture != m2.Texture) return false;
            if (m1.Color != m2.Color) return false;
            if (m1.Specular != m2.Specular) return false;
            if (m1.Ambient != m2.Ambient) return false;
            if (m1.Emmision != m2.Emmision) return false;
            if (m1.Shine != m2.Shine) return false;
            return true;
        }
        #endregion
        //--------------------------------------------------------//
    }
    //------------------------------------------------------------//
}
